package karileet.meeting.profile;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * Main activity class for interactions done inside app
 * 
 * @author markus karileet
 */
public class MainActivity extends Activity {
	public static final String PREFS_NAME = "MeetingProfilePrefsFile";
	public static String APP = "Meeting profile";
	public ProfileState state;
	private CheckBox tb;
	private CheckBox tv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        state = new ProfileState(this);
        setContentView(R.layout.activity_main);
        final SeekBar sb = (SeekBar) findViewById(R.id.beepVolume);
        sb.setMax(15);
        sb.setProgress(state.getBeepVolume());
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				//store new beep volume
				state.setBeepVolume(arg0.getProgress());
				//and play example
				SoundManager sm = new SoundManager(getBaseContext());
				sm.play();
			} 
        	
        });
        tb = (CheckBox) findViewById(R.id.toggle_profile_switch);
        tv = (CheckBox) findViewById(R.id.toggle_vibrate_switch);
        Button rate = (Button) findViewById(R.id.rate_btn);
        rate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(
					Intent.ACTION_VIEW, 
					Uri.parse("https://play.google.com/store/apps/details?id=karileet.meeting.profile")
				);
				startActivity(browserIntent);
			}
		});
        final MeetingProfile mp = new MeetingProfile(this);
		disableVibrateToggleWhenActive();
		tb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				state.setProfileEnabled(tb.isChecked());
				disableVibrateToggleWhenActive();
				RemoteViews views = new RemoteViews(getPackageName(),
						R.layout.meetingprofilewidget);
				//set correct widget graphic when widget is added while state is enabled
				if (tb.isChecked()) {
					mp.enable(state);
					views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_down);
				} else {
					mp.disable(state);
					views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_up);
				}
				ComponentName widget = new ComponentName(getApplicationContext(),
						MeetingProfileWidgetProvider.class);
				(AppWidgetManager.getInstance(getApplicationContext())).updateAppWidget(widget,
						views);
			}
		});
		tv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				state.setVibrateEnabled(tv.isChecked());
				mp.update(state);
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	state = new ProfileState(this);
    	tv.setChecked(state.isVibrateEnabled());
    	tb.setChecked(state.isProfileEnabled());
    	disableVibrateToggleWhenActive();
    }

	/**
	 * Method disables vibrate toggle in UI
	 */
	private void disableVibrateToggleWhenActive() {
//		Log.d(APP, "Starting 'disableVibratorCbWhenActive' method");
//		CheckBox tv = (CheckBox) findViewById(R.id.toggle_vibrate_switch);
//		CheckBox tp = (CheckBox) findViewById(R.id.toggle_profile_switch);
//		tv.setEnabled(!tp.isChecked());
	}    
}
